﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Linq.Dynamic;
using System.Web.Mvc;
using SDT.Model;
using SDT.Lib.Common;
using SDT.Admin.Properties;
using SDT.Admin.ViewModels;
using SDT.Lib.Paging;
using System.Web.UI.WebControls;
using System.IO;
using System.Data.Linq;
using SDT.Lib.Helpers;
namespace SDT.Admin.Controllers
{
    [RoleHelper(Roles = RoleSystem.Dictionary)]
    public class ProviderController : Controller
    {
        //
        // GET: /Provider/

        public ActionResult Index()
        {
            var pageIndex = Request["page"] != null ? Convert.ToInt32(Request["page"]) : 1;
            var pageSize = Request["pageSize"] != null ? Convert.ToInt32(Request["pageSize"]) : 20;
            
            ProviderViewModels p = new ProviderViewModels();
            p.Keyword = Request["Key"] != null ? Request["Key"].ToString() : string.Empty;
            var l = ProviderManager.SelectList(string.Empty);
            p.Providers = l.ToPagedList(pageIndex - 1, pageSize);   

            return View(p);
        }
        [HttpPost]
        public ActionResult Index(ProviderViewModels p)
        {
            var pageIndex = Request["page"] != null ? Convert.ToInt32(Request["page"]) : 1;
            var pageSize = Request["pageSize"] != null ? Convert.ToInt32(Request["pageSize"]) : 20;
            p.Keyword = Request["Key"] != null ? Request["Key"].ToString() : string.Empty;
            var l = ProviderManager.SelectList(string.Empty);
            p.Providers = l.ToPagedList(pageIndex - 1, pageSize);

            return View(p);
        }
        //
        // GET: /Provider/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Provider/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Provider/Create

        [HttpPost]
        public ActionResult Create(ProviderViewModels p)
        {
            try
            {
                // TODO: Add insert logic here
                foreach (string inputTagName in Request.Files)
                {
                    HttpPostedFileBase file = Request.Files[inputTagName];
                    p.Provider.LogoPath = file.FileName;
                }
                p.Provider.AddBy = UserManager.GetUserId();
                p.Provider.AddDate = DateTime.Now;
                p.Provider.ModBy = UserManager.GetUserId();
                p.Provider.ModDate = DateTime.Now;

                ProviderManager.Add(p.Provider);
                foreach (string inputTagName in Request.Files)
                {
                    HttpPostedFileBase file = Request.Files[inputTagName];
                    if (file.ContentLength > 0)
                    {
                        Uploads.UploadFile(file, string.Format("{0}\\{1}", Settings.Default.SiteRootPath, Settings.Default.ProviderRootPath), "" + p.Provider.ID);
                    }
                }
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Provider/Edit/5
 
        public ActionResult Edit(int id)
        {
            ProviderViewModels p = new ProviderViewModels();
            p.Provider = ProviderManager.GetOne(id);
            return View(p);
        }

        //
        // POST: /Provider/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, ProviderViewModels p)
        {
            try
            {
                p.Provider = ProviderManager.GetOne(id);
                UpdateModel(p);
                p.Provider.ModBy = UserManager.GetUserId();
                p.Provider.ModDate = DateTime.Now;
                foreach (string inputTagName in Request.Files)
                {
                    HttpPostedFileBase file = Request.Files[inputTagName];
                    if (file.ContentLength > 0)
                    {
                        p.Provider.LogoPath = file.FileName;
                    }
                }
                ProviderManager.Update(p.Provider);
                foreach (string inputTagName in Request.Files)
                {
                    HttpPostedFileBase file = Request.Files[inputTagName];
                    if (file.ContentLength > 0)
                    {
                        if (file.ContentLength > 0)
                        {
                            Uploads.UploadFile(file, string.Format("{0}\\{1}", Settings.Default.SiteRootPath, Settings.Default.ProviderRootPath), "" + p.Provider.ID);
                        }
                    }
                }
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Provider/Delete/5
 
        public ActionResult Delete(int id)
        {
            Provider p = ProviderManager.GetOne(id);
            if (p == null)
            {
               ViewData["MessageInfo"] = Messages.ERR_NOT_EXIST;
            }
            return View(p);            
        }

        //
        // POST: /Provider/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                Provider p = ProviderManager.GetOne(id);
                if (p == null)
                {
                    ViewData[Messages.MessageInfo] = Messages.ERR_NOT_EXIST;
                }
                if (p != null)
                {
                    //Delete image
                    if (p.LogoPath != null && p.LogoPath != string.Empty)
                    {
                        var path = Settings.Default.SiteRootPath + Settings.Default.ProviderRootPath + p.ID + "_" + p.LogoPath;
                        Uploads.DeleteFile(path);                        
                    }
                    ProviderManager.Delete(id);
                }
                return RedirectToAction("Index");
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }        
    }
}
